//
//  UIViewExtension.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/4.
//

import UIKit

extension UIView {
    
    /// 角度，边框，阴影
    func wn_cornerWithBorder(_ radi: CGFloat, _ borderWidth: CGFloat = 0, _ borderColor: UIColor = .white, _ opacity: Float = 1, _ shadowColor: UIColor = .clear, _ shadowOffset: CGSize = .zero, _ shadowRadius: CGFloat = 0, _ shadowOpacity: Float = 1) {
        self.layer.cornerRadius = radi
        self.layer.borderWidth = borderWidth
        self.layer.borderColor = borderColor.cgColor
        self.layer.opacity = opacity
        self.layer.shadowColor = shadowColor.cgColor
        self.layer.shadowOffset = shadowOffset
        self.layer.shadowRadius = shadowRadius
        self.layer.shadowOpacity = shadowOpacity
        self.layer.masksToBounds = true
    }
    
    /// 自定义圆角
    func wn_cornerCustom(_ corners: UIRectCorner, _ radi: CGFloat) {
        let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radi, height: radi))
        let layer = CAShapeLayer()
        layer.frame = self.bounds
        layer.path = maskPath.cgPath
        self.layer.mask = layer
        self.layer.masksToBounds = true
    }
}

//MARK: - 转圈方法
extension UIView {
    func wn_beginRotation(_ duration: Double = 10, _ isCum: Bool = true) {
        let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotationAnimation.toValue = NSNumber(floatLiteral: Double.pi * 2.0)
        rotationAnimation.duration = duration
        rotationAnimation.isCumulative = isCum
        rotationAnimation.repeatCount = MAXFLOAT
        rotationAnimation.isRemovedOnCompletion = false
        layer.add(rotationAnimation, forKey: "rotationAnimation")
        layer.speed = 1
    }
    
    func wn_pauseRotation(_ pause: Bool) {
        layer.speed = pause ? 0 : 1
    }
    
    func wn_stopRotation() {
        layer.speed = 0
        layer.removeAnimation(forKey: "rotationAnimation")
    }
}

extension UIView {
    func wn_refreshHeader(completion: @escaping ReturnClosure) -> MJRefreshGifHeader {
        let header = MJRefreshGifHeader {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
                completion()
            })
        }
        let arr = WNAnimation.wn_refLoadingArray
        header.setImages(arr, duration: TimeInterval(arr.count)/25, for: .refreshing)
        header.stateLabel?.isHidden = true
        header.lastUpdatedTimeLabel?.isHidden = true
        header.setTitle("Getting data...", for: .idle)
        return header
    }
    
    func wn_refreshFooter(_ footerStr: String = "Anyway, there's no more data", completion: @escaping ReturnClosure) -> MJRefreshBackGifFooter {
        let footer = MJRefreshBackGifFooter {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
                completion()
            })
        }
        footer.stateLabel?.textColor = .init(named: "gray_CECECE")
        footer.stateLabel?.font = .wn_font(name: .bold, size: 11)
        footer.setTitle("", for: .idle)
        footer.setTitle(footerStr, for: .noMoreData)
        return footer
    }
}

extension UIView {
    var x: CGFloat {
        get{
            return self.frame.origin.x
        } set(value) {
            self.frame.origin.x = value
        }
    }
    
    var y: CGFloat {
        get{
            return self.frame.origin.y
        } set(value) {
            self.frame.origin.y = value
        }
    }
    
    var width: CGFloat {
        get{
            return self.frame.width
        } set(value) {
            self.frame.size.width = value
        }
    }
    
    var height: CGFloat {
        get{
            return self.frame.height
        } set(value) {
            self.frame.size.height = value
        }
    }
    
    var top: CGFloat {
        get{
            return self.y
        } set(value) {
            self.y = value
        }
    }
    
    var left: CGFloat {
        get{
            return self.x
        } set(value) {
            self.x = value
        }
    }
    
    var bottom: CGFloat {
        get{
            return self.y + self.height
        } set(value) {
            self.y = value - self.height
        }
    }
    
    var right: CGFloat {
        get{
            return self.x + self.width
        } set(value) {
            self.x = value - self.width
        }
    }
    
    var centerX: CGFloat {
        get {
            return self.center.x
        } set(value) {
            self.center.x = value
        }
    }
    
    var centerY: CGFloat {
        get {
            return self.center.y
        } set(value) {
            self.center.y = value
        }
    }
    
    var origin: CGPoint {
        get{
            return self.frame.origin
        } set(value) {
            self.frame.origin = value
        }
    }
    
    var size: CGSize {
        get{
            return self.frame.size
        } set(value) {
            self.frame.size = value
        }
    }
}
